/**
 * @NApiVersion 2.x
 * @NScriptType Restlet
 */
define(['N/record'], function (record) {

    var attribute = new Object();
    var method = new Object();

    function _get(context) {
    }

    function post(context) {

        var responseBody = [];
        var orders = context.orders;
        for (var index in orders) {
            var order = orders[index];
            order.platform = context.platform;

            var result = {};
            result["uniqueId"] = order.uniqueId;
            var message;
            var success;
            try {
                var objId =
                    method.saveSource(order);
                success = true;
                message = "ns source id :" + objId;
            } catch (e) {
                success = false;
                message = JSON.stringify(e);
            }
            result["message"] = message;
            result["success"] = success;
            responseBody.push(result);
        }
        return responseBody;
    }

    method.saveSource = function (order) {
        var obj = record.create({
            type: 'customrecord_platform_so_source',
            isDynamic: true
        });
        try {
            obj.setValue('custrecord_platform_so_platform', order.platform);
        } catch (e) {
        }
        try {
            obj.setValue('custrecord_platform_so_order_id', order.orderId);
        } catch (e) {
        }
        try {
            obj.setValue('custrecord_platform_so_date', new Date(order.purchaseTime));
        } catch (e) {
        }
        try {
            obj.setValue('custrecord_platform_so_context', JSON.stringify(order));
        } catch (e) {
        }
        try {
            obj.setValue('custrecord_platform_so_status', 1);
        } catch (e) {
        }
        return obj.save();
    }

    return {
        get: _get,
        post: post
    };
});